<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>sha2_64bit_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_crypt_sha2_64bit_test.js.html">sha2_64bit_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2014 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.crypt.Sha2_64bit_test&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.crypt.Sha2_64bit_test&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.array&#39;);
<a name="line19"></a>goog.require(&#39;goog.crypt&#39;);
<a name="line20"></a>goog.require(&#39;goog.crypt.Sha384&#39;);
<a name="line21"></a>goog.require(&#39;goog.crypt.Sha512&#39;);
<a name="line22"></a>goog.require(&#39;goog.crypt.Sha512_256&#39;);
<a name="line23"></a>goog.require(&#39;goog.crypt.hashTester&#39;);
<a name="line24"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line25"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>/**
<a name="line29"></a> * Simple sanity tests for hash functions.
<a name="line30"></a> */
<a name="line31"></a>function testBasicOperations() {
<a name="line32"></a>  var sha512 = new goog.crypt.Sha512();
<a name="line33"></a>  goog.crypt.hashTester.runBasicTests(sha512);
<a name="line34"></a>
<a name="line35"></a>  var sha384 = new goog.crypt.Sha384();
<a name="line36"></a>  goog.crypt.hashTester.runBasicTests(sha384);
<a name="line37"></a>
<a name="line38"></a>  var sha256 = new goog.crypt.Sha512_256();
<a name="line39"></a>  goog.crypt.hashTester.runBasicTests(sha256);
<a name="line40"></a>}
<a name="line41"></a>
<a name="line42"></a>
<a name="line43"></a>/**
<a name="line44"></a> * Each object in the test vector array is a source text and one or more
<a name="line45"></a> * hashes of that source text.  The source text is either a string or a
<a name="line46"></a> * byte array.
<a name="line47"></a> * &lt;p&gt;
<a name="line48"></a> * All hash values, except for the empty string, are from public sources:
<a name="line49"></a> *   csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf
<a name="line50"></a> *   csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA384.pdf
<a name="line51"></a> *   csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA512_256.pdf
<a name="line52"></a> *   csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA2_Additional.pdf
<a name="line53"></a> *   en.wikipedia.org/wiki/SHA-2#Examples_of_SHA-2_variants
<a name="line54"></a> */
<a name="line55"></a>var testVector = [
<a name="line56"></a>  {
<a name="line57"></a>    // Make sure the algorithm correctly handles the empty string
<a name="line58"></a>    source:
<a name="line59"></a>        &#39;&#39;,
<a name="line60"></a>    512:
<a name="line61"></a>        &#39;cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce&#39; +
<a name="line62"></a>        &#39;47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e&#39;
<a name="line63"></a>  },
<a name="line64"></a>  {
<a name="line65"></a>    source:
<a name="line66"></a>        &#39;abc&#39;,
<a name="line67"></a>    512:
<a name="line68"></a>        &#39;ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a&#39; +
<a name="line69"></a>        &#39;2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f&#39;,
<a name="line70"></a>    384:
<a name="line71"></a>        &#39;cb00753f45a35e8bb5a03d699ac65007272c32ab0eded163&#39; +
<a name="line72"></a>        &#39;1a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7&#39;,
<a name="line73"></a>    256:
<a name="line74"></a>        &#39;53048e2681941ef99b2e29b76b4c7dabe4c2d0c634fc6d46e0e2f13107e7af23&#39;
<a name="line75"></a>  },
<a name="line76"></a>  {
<a name="line77"></a>    source:
<a name="line78"></a>        &#39;abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn&#39; +
<a name="line79"></a>        &#39;hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu&#39;,
<a name="line80"></a>    512:
<a name="line81"></a>        &#39;8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018&#39; +
<a name="line82"></a>        &#39;501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909&#39;,
<a name="line83"></a>    384:
<a name="line84"></a>        &#39;09330c33f71147e83d192fc782cd1b4753111b173b3b05d2&#39; +
<a name="line85"></a>        &#39;2fa08086e3b0f712fcc7c71a557e2db966c3e9fa91746039&#39;,
<a name="line86"></a>    256:
<a name="line87"></a>        &#39;3928e184fb8690f840da3988121d31be65cb9d3ef83ee6146feac861e19b563a&#39;
<a name="line88"></a>  },
<a name="line89"></a>  {
<a name="line90"></a>    source:
<a name="line91"></a>        &#39;The quick brown fox jumps over the lazy dog&#39;,
<a name="line92"></a>    512:
<a name="line93"></a>        &#39;07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb64&#39; +
<a name="line94"></a>        &#39;2e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6&#39;,
<a name="line95"></a>    384:
<a name="line96"></a>        &#39;ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c49&#39; +
<a name="line97"></a>        &#39;4011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1&#39;,
<a name="line98"></a>    256:
<a name="line99"></a>        &#39;dd9d67b371519c339ed8dbd25af90e976a1eeefd4ad3d889005e532fc5bef04d&#39;
<a name="line100"></a>  },
<a name="line101"></a>  {
<a name="line102"></a>    // Make sure the algorithm correctly handles long strings
<a name="line103"></a>    source:
<a name="line104"></a>        goog.array.repeat(0, 1000000),
<a name="line105"></a>    512:
<a name="line106"></a>        &#39;ce044bc9fd43269d5bbc946cbebc3bb711341115cc4abdf2edbc3ff2c57ad4b1&#39; +
<a name="line107"></a>        &#39;5deb699bda257fea5aef9c6e55fcf4cf9dc25a8c3ce25f2efe90908379bff7ed&#39;
<a name="line108"></a>  }
<a name="line109"></a>];
<a name="line110"></a>
<a name="line111"></a>
<a name="line112"></a>/**
<a name="line113"></a> * Function called by the actual testers to ensure that specific strings
<a name="line114"></a> * hash to specific published values.
<a name="line115"></a> *
<a name="line116"></a> * Each item in the vector has a &quot;source&quot; and one or more additional keys.
<a name="line117"></a> * If the item has a key matching the key argument passed to this
<a name="line118"></a> * function, it is the expected value of the hash function.
<a name="line119"></a> *
<a name="line120"></a> * @param {goog.crypt.Sha2_64bit} hasher The hasher to test
<a name="line121"></a> * @param {string} key The key in testVector whose value represents the
<a name="line122"></a> *     expected result of this hash on the source.
<a name="line123"></a> */
<a name="line124"></a>function hashGoldenTester(hasher, key) {
<a name="line125"></a>  count = 0;
<a name="line126"></a>  goog.array.forEach(testVector, function(data) {
<a name="line127"></a>    if (data[key]) {
<a name="line128"></a>      hasher.reset();
<a name="line129"></a>      hasher.update(data.source);
<a name="line130"></a>      var digest = hasher.digest();
<a name="line131"></a>      var expected = goog.crypt.hexToByteArray(data[key]);
<a name="line132"></a>      assertElementsEquals(expected, digest);
<a name="line133"></a>      count++;
<a name="line134"></a>    }
<a name="line135"></a>  });
<a name="line136"></a>  // There must be a typo if no tests are run.  This assertion assures that
<a name="line137"></a>  // the test code is correct.
<a name="line138"></a>  assertTrue(&#39;Error in testing.  At least one test should be run&#39;,
<a name="line139"></a>      count &gt; 0);
<a name="line140"></a>}
<a name="line141"></a>
<a name="line142"></a>
<a name="line143"></a>/** Test that Sha512() returns the published values */
<a name="line144"></a>function testHashing512() {
<a name="line145"></a>  // This test tends to time out on IE7.
<a name="line146"></a>  if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(&#39;8&#39;)) {
<a name="line147"></a>    hashGoldenTester(new goog.crypt.Sha512(), &#39;512&#39;);
<a name="line148"></a>  }
<a name="line149"></a>}
<a name="line150"></a>
<a name="line151"></a>
<a name="line152"></a>/** Test that Sha384 returns the published values */
<a name="line153"></a>function testHashing384() {
<a name="line154"></a>  hashGoldenTester(new goog.crypt.Sha384(), &#39;384&#39;);
<a name="line155"></a>}
<a name="line156"></a>
<a name="line157"></a>
<a name="line158"></a>/** Test that Sha512_256 returns the published values */
<a name="line159"></a>function testHashing256() {
<a name="line160"></a>  hashGoldenTester(new goog.crypt.Sha512_256(), &#39;256&#39;);
<a name="line161"></a>}
<a name="line162"></a>
<a name="line163"></a>
<a name="line164"></a>/** Check that the code throws an error for bad input */
<a name="line165"></a>function testBadInput() {
<a name="line166"></a>  assertThrows(&#39;Bad input&#39;, function() {
<a name="line167"></a>    new goog.crypt.Sha512().update({});
<a name="line168"></a>  });
<a name="line169"></a>  assertThrows(&#39;Floating point not allows&#39;, function() {
<a name="line170"></a>    new goog.crypt.Sha512().update([1, 2, 3, 4, 4.5]);
<a name="line171"></a>  });
<a name="line172"></a>  assertThrows(&#39;Negative not allowed&#39;, function() {
<a name="line173"></a>    new goog.crypt.Sha512().update([1, 2, 3, 4, -10]);
<a name="line174"></a>  });
<a name="line175"></a>  assertThrows(&#39;Must be byte array&#39;, function() {
<a name="line176"></a>    new goog.crypt.Sha512().update([1, 2, 3, 4, {}]);
<a name="line177"></a>  });
<a name="line178"></a>}
</pre>


</body>
</html>
